<template>
  <avue-crud :option="option"
             :table-loading="loading"
             :data="data"
             v-model:page="page"
             v-model="form"
             ref="crud"
             @row-save="rowSave"
             @search-change="searchChange"
             @search-reset="searchReset"
             @selection-change="selectionChange"
             @current-change="currentChange"
             @size-change="sizeChange"
             @on-load="onLoad">
    <template #groupId="{ row }">
      <el-tag size="large" style="cursor: pointer" @click="viewDetail(row)">{{ row.groupName }}</el-tag>
    </template>
    <template #menu-left>
      <el-button type="danger"
                 icon="el-icon-circle-close"
                 plain
                 @click="unbindGroups">解绑分组
      </el-button>
    </template>
    <template #menu="scope">
      <el-button text
                 type="primary"
                 icon="el-icon-view"
                 @click.stop="viewDetail(scope.row)">
        查看
      </el-button>
      <el-button text
                 type="primary"
                 icon="el-icon-circle-close"
                 @click.stop="unbindGroup(scope.row)">
        解绑
      </el-button>
    </template>
  </avue-crud>
</template>

<script>
import { getTree, getPage, addGroupDevice, unbindGroupDevice } from '@/api/iot/group';
import {validatenull} from "@/utils/validate";


export default {
  name: 'deviceGroup',
  props: {
    productKey: String,
    deviceId: String,
  },
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 172,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        editBtn: false,
        delBtn: false,
        viewBtn: false,
        selection: true,
        menu: true,
        addBtnText: '添加分组',
        emptyText: '当前设备尚未添加任何分组',
        dialogWidth: 500,
        labelWidth: 120,
        searchLabelWidth: 100,
        column: [
          {
            label: "设备分组",
            prop: "groupId",
            type: "tree",
            dicData: [],
            props: {
              label: "title",
            },
            slot: true,
            filterable: true,
            checkStrictly: true,
            span: 24,
          },
          {
            label: "分组ID",
            prop: "id",
            type: "input",
            display: false,
            span: 24,
          },
          {
            label: "添加时间",
            prop: "createTime",
            type: "input",
            display: false,
            span: 24,
          },
        ]
      },
      data: []
    };
  },
  watch: {
    deviceId() {
      if (!validatenull(this.deviceId)) {
        this.onLoad(this.page);
      }
    }
  },
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getTree().then(res => {
        const column = this.findObject(this.option.column, "groupId");
        column.dicData = res.data.data;
      });
    },
    rowSave(row, done, loading) {
      addGroupDevice(this.deviceId, row.groupId).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    viewDetail(row) {
      window.open(`/iot/group/detail/${row.id}`);
    },
    unbindGroup(row) {
      this.$confirm(`是否解绑所选设备分组[${row.groupName}]?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        unbindGroupDevice(this.deviceId, row.id).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
      });
    },
    unbindGroups() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据解绑?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        unbindGroupDevice(this.deviceId, this.ids).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
      });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      if (validatenull(this.deviceId)) {
        return;
      }
      this.loading = true;
      getPage(page.currentPage, page.pageSize, this.deviceId).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style>
</style>
